package br.com.massai.model.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "FUNCTION")
public class Function extends GenericEntity {

	private static final long serialVersionUID = 4467759201200662090L;

	@Id
	@SequenceGenerator(name = "seqFunction", sequenceName = "seq_function", allocationSize = 1)
	@GeneratedValue(generator = "seqFunction", strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(name = "ACRONYM", unique = true, length = 50, columnDefinition = "VARCHAR(50)")
	private String acronym;

	@Column(name = "DESCRIPTION", unique = true, length = 150, columnDefinition = "VARCHAR(150)")
	private String description;

	@OneToMany(mappedBy = "function", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private List<Url> urls;

	@LazyCollection(LazyCollectionOption.FALSE)
	@OneToMany(mappedBy = "function", cascade = CascadeType.ALL)
	@Fetch(FetchMode.SUBSELECT)
	private List<Action> actions;

	@ManyToOne(optional = false)
	@JoinColumn(name = "MODULE_ID", nullable = false, referencedColumnName = "ID")
	private Module module;

	@Transient
	private boolean selected;

	public Function() {
		actions = new ArrayList<Action>();
		urls = new ArrayList<Url>();
		module = new Module();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof Function)
			if(((Function)obj).getId().equals(this.getId())) return true;

		return false;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAcronym() {
		return acronym;
	}

	public void setAcronym(String acronym) {
		this.acronym = acronym;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Url> getUrls() {
		return urls;
	}

	public void setUrls(List<Url> urls) {
		this.urls = urls;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public List<Action> getActions() {
		return actions;
	}

	public void setActions(List<Action> actions) {
		this.actions = actions;
	}
}